namespace TwitterButtler.RequestServers
{
	#region #using Directives

	using System.Net;
	using System.Net.Sockets;

	#endregion

	public static class WakeOnLanMessage
	{
		/// <summary>
		/// Sends a Wake-On-Lan packet to the specified MAC address.
		/// </summary>
		/// <param name="mac">Physical MAC address to send WOL packet to.</param>
		public static void WakeUp(byte[] mac)
		{
			//
			// WOL packet is sent over UDP 255.255.255.0:40000.
			//
			var client = new UdpClient();
			client.Connect(IPAddress.Broadcast, 40000);

			//
			// WOL packet contains a 6-bytes trailer and 16 times a 6-bytes sequence containing the MAC address.
			//
			var packet = new byte[17 * 6];

			//
			// Trailer of 6 times 0xFF.
			//
			for (int i = 0; i < 6; i++)
			{
				packet[i] = 0xFF;
			}

			//
			// Body of magic packet contains 16 times the MAC address.
			//
			for (int i = 1; i <= 16; i++)
			{
				for (int j = 0; j < 6; j++)
				{
					packet[i * 6 + j] = mac[j];
				}
			}

			//
			// Submit WOL packet.
			//
			client.Send(packet, packet.Length);
		}
	}
}